﻿/*CLR Version: 4.0.30319.18408
 * Creat Date: 2015/6/18 16:08:36
 * Creat Year: 2015
 * Creator: 程炜.CW
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Utility.Extensions
{
    /// <summary>
    /// 枚举类型Description Attribut 获取
    /// </summary>
    public static class EnumDescriptionAttributeExtensions
    {
        /// <summary>
        /// 获取 DescriptionAttribute 的 Description 值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            System.Reflection.MemberInfo[] mi = value.GetType().GetMember(value.ToString());
            if (null != mi && mi.Length > 0)
            {
                IEnumerable<DescriptionAttribute> attributes = mi[0].GetCustomAttributes<DescriptionAttribute>();
                if (attributes != null && attributes.Count() > 0)
                {
                    return attributes.ToList()[0].Description;
                }
                else
                {
                    return value.ToString();
                }
            }
            else
            {
                return value.ToString(); 
            }
        }
    }
}
